package com.ymatou.iapi.godmch.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.godmch.parameter.PercentageItem;
import com.ymatou.iapi.godmch.parameter.applyAgentBean;
import com.ymatou.iapi.godmch.parameter.createagentBean;
import com.ymatou.iapi.godmch.parameter.setAgentMarkupConfigBean;
import com.ymatou.iapi.godmch.service.applyAgentCall;
import com.ymatou.iapi.godmch.service.createagentCall;
import com.ymatou.iapi.godmch.service.setAgentMarkupConfigCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.model.godmch.markup_config_agent_price;
import com.ymttest.database.model.godmch.markup_config_agent_priceExample;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
import com.ymttest.database.sqlwapper.godmch.markup_config_agent_priceWapper;
/**
*设置代理价全局加价规则
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_setAgentMarkupConfig{
	private static setAgentMarkupConfigBean bean;
	private static setAgentMarkupConfigCall call;
	public static agent_typeWapper agentTypeWapper = new agent_typeWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("设置代理价全局加价规则");
	}

	@Before
	public void caseUp() {
		bean=new setAgentMarkupConfigBean();
		call=new setAgentMarkupConfigCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void check_verify() throws InterruptedException {
		
		Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		markup_config_agent_priceWapper priceWapper=new markup_config_agent_priceWapper();
		markup_config_agent_priceExample priceExample=new markup_config_agent_priceExample();
		priceExample.createCriteria().andMch_idEqualTo(bean.getMchId());
		markup_config_agent_price agentPrice=priceWapper.selectByExample(priceExample).get(0);
		Logger.verifyEquals(true, agentPrice.getGlobal_config(), "验证markup_config_agent_price中的global_config");
		Logger.verifyEquals(0, agentPrice.getCondition_type(), "验证markup_config_agent_price中的Condition_type");
		Logger.verifyEquals(bean.getDecimalType(), agentPrice.getDecimal_type(), "验证markup_config_agent_price中的Decimal_type");
		Logger.verifyEquals(bean.getRefreshProduct(), agentPrice.getRefresh_product(), "验证markup_config_agent_price中的Refresh_product");
		Logger.verifyEquals(0, agentPrice.getConfig_status(), "验证markup_config_agent_price中的Config_status");
		Logger.verifyEquals(null, agentPrice.getCondition_list(), "验证markup_config_agent_price中的Condition_list");

	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setAgentMarkupConfig_001() {
		Logger.start(true,"设置代理价全局加价规则-商户代理类型不存在-新增");
		try {
			Integer mchId=GodmchManageCallService.CreateSellerMch();
            //查询代理类型
			Map<String, Object> map = new HashMap<>();
			map.put("mchId", mchId);
			List<agent_type> list = agentTypeWapper.selectby(map);
			
			List<PercentageItem> items=new ArrayList();
			PercentageItem item=new PercentageItem();
			item.setAgentTypeId(list.get(0).getAgentType());
			item.setPercentage(new BigDecimal(10));
			items.add(item);
			bean.setMchId(mchId);
			bean.setPercentageList(items);
			bean.setDecimalType(1);
			bean.setRefreshProduct(false);
			call.setData(bean);
			call.callService();	
			
			check_verify();
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setAgentMarkupConfig_002() {
		Logger.start(true,"设置代理价全局加价规则-商户代理类型存在的-修改");
		try {
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			Thread.sleep(5000);
            //查询代理类型
			Map<String, Object> map = new HashMap<>();
			map.put("mchId", mchId);
			List<agent_type> list = agentTypeWapper.selectby(map);
			
			List<PercentageItem> items=new ArrayList();
			PercentageItem item=new PercentageItem();
			item.setAgentTypeId(list.get(0).getAgentType());
			item.setPercentage(new BigDecimal(10));
			items.add(item);
			bean.setMchId(mchId);
			bean.setPercentageList(items);
			bean.setDecimalType(1);
			bean.setRefreshProduct(false);
			call.setData(bean);
			call.callService();	
			//修改
			bean.setRefreshProduct(true);
			call.setData(bean);
			call.callService();	
			
			check_verify();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setAgentMarkupConfig_003() {
		Logger.start(true,"设置代理价全局加价规则-商户与代理类型关系不存在-返回失败");
		try {
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			Thread.sleep(5000);
            //查询代理类型
			Map<String, Object> map = new HashMap<>();
			map.put("mchId", mchId);
			List<agent_type> list = agentTypeWapper.selectby(map);
			
			List<PercentageItem> items=new ArrayList();
			PercentageItem item=new PercentageItem();
			item.setAgentTypeId(list.get(0).getAgentType());
			item.setPercentage(new BigDecimal(10));
			items.add(item);
			bean.setMchId(mchId+111);
			bean.setPercentageList(items);
			bean.setDecimalType(1);//加价后小数点类型 1：精确到个位数 2：保留小数点后1位 3：保留小数点后2位 4：精确到十位数
			bean.setRefreshProduct(false);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("500", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setAgentMarkupConfig_005() {
		Logger.start(true,"设置代理价全局加价规则-加价后小数点类型2保留小数点后1位-返回成功");
		try {
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			Thread.sleep(5000);
            //查询代理类型
			Map<String, Object> map = new HashMap<>();
			map.put("mchId", mchId);
			List<agent_type> list = agentTypeWapper.selectby(map);
			
			List<PercentageItem> items=new ArrayList();
			PercentageItem item=new PercentageItem();
			item.setAgentTypeId(list.get(0).getAgentType());
			item.setPercentage(new BigDecimal(10));
			items.add(item);
			bean.setMchId(mchId);
			bean.setPercentageList(items);
			bean.setDecimalType(2);//加价后小数点类型 1：精确到个位数 2：保留小数点后1位 3：保留小数点后2位 4：精确到十位数
			bean.setRefreshProduct(false);
			call.setData(bean);
			call.callService();				
			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setAgentMarkupConfig_006() {
		Logger.start(true,"设置代理价全局加价规则-加价后小数点类型3保留小数点后2位-返回成功");
		try {
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			Thread.sleep(5000);
            //查询代理类型
			Map<String, Object> map = new HashMap<>();
			map.put("mchId", mchId);
			List<agent_type> list = agentTypeWapper.selectby(map);
			
			List<PercentageItem> items=new ArrayList();
			PercentageItem item=new PercentageItem();
			item.setAgentTypeId(list.get(0).getAgentType());
			item.setPercentage(new BigDecimal(10));
			items.add(item);
			bean.setMchId(mchId);
			bean.setPercentageList(items);
			bean.setDecimalType(3);//加价后小数点类型 1：精确到个位数 2：保留小数点后1位 3：保留小数点后2位 4：精确到十位数
			bean.setRefreshProduct(false);
			call.setData(bean);
			call.callService();				
			
			check_verify();		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setAgentMarkupConfig_007() {
		Logger.start(true,"设置代理价全局加价规则-加价后小数点类型4精确到十位数-返回成功");
		try {
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			Thread.sleep(5000);
            //查询代理类型
			Map<String, Object> map = new HashMap<>();
			map.put("mchId", mchId);
			List<agent_type> list = agentTypeWapper.selectby(map);
			
			List<PercentageItem> items=new ArrayList();
			PercentageItem item=new PercentageItem();
			item.setAgentTypeId(list.get(0).getAgentType());
			item.setPercentage(new BigDecimal(10));
			items.add(item);
			bean.setMchId(mchId);
			bean.setPercentageList(items);
			bean.setDecimalType(4);//加价后小数点类型 1：精确到个位数 2：保留小数点后1位 3：保留小数点后2位 4：精确到十位数
			bean.setRefreshProduct(false);
			call.setData(bean);
			call.callService();				
			
			check_verify();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setAgentMarkupConfig_008() {
		Logger.start(true,"设置代理价全局加价规则-加价后小数点类型不存在-返回失败");
		try {
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			Thread.sleep(5000);
            //查询代理类型
			Map<String, Object> map = new HashMap<>();
			map.put("mchId", mchId);
			List<agent_type> list = agentTypeWapper.selectby(map);
			
			List<PercentageItem> items=new ArrayList();
			PercentageItem item=new PercentageItem();
			item.setAgentTypeId(list.get(0).getAgentType());
			item.setPercentage(new BigDecimal(10));
			items.add(item);
			bean.setMchId(mchId);
			bean.setPercentageList(items);
			bean.setDecimalType(5);//加价后小数点类型 1：精确到个位数 2：保留小数点后1位 3：保留小数点后2位 4：精确到十位数
			bean.setRefreshProduct(false);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("500", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setAgentMarkupConfig_009() {
		Logger.start(true,"设置代理价全局加价规则-覆盖已添加的商品价格refreshProduct=true-返回成功");
		try {
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			Thread.sleep(5000);
            //查询代理类型
			Map<String, Object> map = new HashMap<>();
			map.put("mchId", mchId);
			List<agent_type> list = agentTypeWapper.selectby(map);
			
			List<PercentageItem> items=new ArrayList();
			PercentageItem item=new PercentageItem();
			item.setAgentTypeId(list.get(0).getAgentType());
			item.setPercentage(new BigDecimal(10));
			items.add(item);
			bean.setMchId(mchId);
			bean.setPercentageList(items);
			bean.setDecimalType(4);//加价后小数点类型 1：精确到个位数 2：保留小数点后1位 3：保留小数点后2位 4：精确到十位数
			bean.setRefreshProduct(true);
			call.setData(bean);
			call.callService();				
			check_verify();	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_setAgentMarkupConfig_011() {
		Logger.start(true,"设置代理价全局加价规则-代理类型关系不存在-返回失败");
		try {
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			Thread.sleep(5000);
			Integer mchId2=GodmchManageCallService.CreateSellerMch();
			Thread.sleep(5000);
            //查询代理类型
			Map<String, Object> map = new HashMap<>();
			map.put("mchId", mchId);
			List<agent_type> list = agentTypeWapper.selectby(map);
			
			List<PercentageItem> items=new ArrayList();
			PercentageItem item=new PercentageItem();
			item.setAgentTypeId(list.get(0).getAgentType());
			item.setPercentage(new BigDecimal(10));
			items.add(item);
			bean.setMchId(mchId2);
			bean.setPercentageList(items);
			bean.setDecimalType(4);//加价后小数点类型 1：精确到个位数 2：保留小数点后1位 3：保留小数点后2位 4：精确到十位数
			bean.setRefreshProduct(true);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("500", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_setAgentMarkupConfig_012() {
		Logger.start(true,"设置代理价全局加价规则-加价百分比为负数-返回失败(目前研发没有控制)");
		try {
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			Thread.sleep(5000);
            //查询代理类型
			Map<String, Object> map = new HashMap<>();
			map.put("mchId", mchId);
			List<agent_type> list = agentTypeWapper.selectby(map);
			
			List<PercentageItem> items=new ArrayList();
			PercentageItem item=new PercentageItem();
			item.setAgentTypeId(list.get(0).getAgentType());
			item.setPercentage(new BigDecimal(-10));
			items.add(item);
			bean.setMchId(mchId);
			bean.setPercentageList(items);
			bean.setDecimalType(4);//加价后小数点类型 1：精确到个位数 2：保留小数点后1位 3：保留小数点后2位 4：精确到十位数
			bean.setRefreshProduct(true);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("500", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}